﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Net.Cache;
using System.Reflection;
namespace UTIL
{
    public class CRss
    {
        public static void DisplayCol(DataGridView dgv, String dataPropertyName, String headerText)
        {
            dgv.AutoGenerateColumns = false;          
            DataGridViewLinkColumn obj = new DataGridViewLinkColumn();
            
            obj.DataPropertyName = dataPropertyName;
            obj.HeaderText = headerText;
            obj.Name = dataPropertyName;
            obj.Resizable = DataGridViewTriState.True;
            dgv.Columns.AddRange(new DataGridViewColumn[] { obj });
        } 

        /// <summary>   
        /// 获取Rss资源   
        /// </summary>   
        /// <param name="RssURL"></param>   
        /// <returns></returns>   
        public static DataTable ReadRss(string RssURL)
        {
            DataTable Dt = new DataTable();
            DataColumn HeaderText = new DataColumn("HeaderText", typeof(string));
            DataColumn Title = new DataColumn("Title", typeof(string));
            DataColumn Link = new DataColumn("Link", typeof(string));
            DataColumn Author = new DataColumn("Author", typeof(string));
            DataColumn PubDate = new DataColumn("PubDate", typeof(string));
            DataColumn RssText = new DataColumn("RssText", typeof(string));
            Dt.Columns.Add(HeaderText);
            Dt.Columns.Add(Title);
            Dt.Columns.Add(Link);
            Dt.Columns.Add(Author);
            Dt.Columns.Add(PubDate);
            Dt.Columns.Add(RssText);
            try
            {
                System.Net.WebRequest myRequest = System.Net.WebRequest.Create(RssURL);
                RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                myRequest.CachePolicy = policy;
                System.Net.WebResponse myResponse = myRequest.GetResponse();

                System.IO.Stream rssStream = myResponse.GetResponseStream();
                System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
                rssDoc.Load(rssStream);

                System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
                for (int i = 0; i < rssItems.Count; i++)
                {
                    DataRow Row = Dt.NewRow();
                    System.Xml.XmlNode rssDetail;
                    //标题   
                    rssDetail = rssItems.Item(i).SelectSingleNode("title");
                    if (rssDetail != null)
                    {
                        Row["Title"] = rssDetail.InnerText;
                    }
                    else
                    {
                        Row["Title"] = "";
                    }
                    //作者   
                    rssDetail = rssItems.Item(i).SelectSingleNode("author");
                    if (rssDetail != null)
                    {
                        Row["Author"] = rssDetail.InnerText;
                    }
                    else
                    {
                        Row["Author"] = "";
                    }
                    //发布时间   
                    rssDetail = rssItems.Item(i).SelectSingleNode("pubDate");
                    if (rssDetail != null && rssDetail.InnerText.Trim()!=string.Empty )
                    {
                        Row["PubDate"] = Convert.ToDateTime(rssDetail.InnerText).ToString("yyyy年MM月dd日");
                    }
                    else
                    {
                        Row["PubDate"] = "";
                    }
                    //链接地址   
                    rssDetail = rssItems.Item(i).SelectSingleNode("link");
                    if (rssDetail != null)
                    {
                        Row["Link"] = rssDetail.InnerText;
                    }
                    else
                    {
                        Row["Link"] = "";
                    }
                    Dt.Rows.Add(Row);
                }

                rssStream.Close();
                myResponse.Close();                
                return Dt;
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                return Dt;
            }
           
        }  

    }
}
